package com.controller;

import java.io.IOException;
import java.io.PrintWriter;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

//不要忘记写@WebServlet注释
@WebServlet("/ca")
public class AreaServlet extends HttpServlet {

	//重写doGet方法
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		this.doPost(req, resp);
	}

	//重写doPost方法
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//设置请求的编码格式 utf-8
		req.setCharacterEncoding("utf-8");
		//设置响应的内容类型 text/html;charset=utf-8
		resp.setContentType("text/html;charset=utf-8");
		//获取传递过来的参数up
		String Up = req.getParameter("up");
		//获取传递过来的参数down
		String Down = req.getParameter("down");
		//获取传递过来的参数height
		String Height = req.getParameter("height");
		//创建一个输出流对象out
		PrintWriter out = resp.getWriter();
		//最后的结果保存到result中
		double result = 0.0;
		
		//如果result结果有效,那么直接输出结果
		try {
result = (Double.parseDouble(Up)+Double.parseDouble(Down))*Double.parseDouble(Height)*0.5;
out.println("<html><body bgcolor='orange'>梯形的面积为"+result+"</body></html>");
	}catch(NumberFormatException e) {
		//如果result结果无效,通过catch语句块来跳转到相应页面
out.println("<html><body bgcolor='pink'>请输入正确的数!<a href='a.html'>重新输入</a></body></html>");
	}

		
  }
		
}
